var 背景颜色 = "#fffff"
var 字号 = "13"
var 字体颜色 = "#dd000000"


var window = floaty.window(
    <vertical padding="8 0 8 0" alpha="0.5">
        <vertical id="输入框"  bg="#ffffff">
            <input id="街道" text="茂林镇" hint="茂林镇" singleLine="true" w="*" focusable="true"/>
            <input id="村居" text="大芦社区"hint="什么社区" singleLine="true" w="*" focusable="true"/>
            <input id="村子" text="大芦" hint="村名" singleLine="true" w="*" focusable="true"/>
            <button id="隐藏" text="-" h="28sp" layout_weight="1"/>
        </vertical>
        <button id="显示" text="-" h="28sp" layout_weight="1"/>
        <linear>
            <button id="puc" text="普" w="34" layout_weight="1"/>
            <button id="kon" text="空" w="34" layout_weight="1"/>
        </linear>
        <button id="action" text="拖动或关闭" size="8sp" w="*" h="34"/>
        
    </vertical>
);

window.exitOnClose();
setInterval(() => {}, 1000);

window.显示.setVisibility(8);

window.街道.on("touch_down", () => {
    window.requestFocus();
    window.街道.requestFocus();
});
window.村居.on("touch_down", () => {
    window.requestFocus();
    window.村居.requestFocus();
});
window.村子.on("touch_down", () => {
    window.requestFocus();
    window.村子.requestFocus();
});

window.隐藏.on("click", () => {
    window.输入框.setVisibility(8) //隐藏
    window.显示.setVisibility(0) //出现
    window.disableFocus()
});

window.显示.on("click", () => {
    window.输入框.setVisibility(0)
    window.显示.setVisibility(8)
});

window.kon.on("click", () => {
    threads.start(function() {
        if (window.kon.getText() == "空") {
            window.kon.setText("...")
            空地();
            window.kon.setText("空")
        } else {
            toastLog("正在弄不需要调查的操作")
        }
    });
});

window.puc.on("click", () => {
    threads.start(function() {
        if (window.puc.getText() == "普") {
            window.puc.setText("...")
            普查();
            window.puc.setText("普")
        } else {
            toastLog("正在执行普查的操作")
        }
    });
});

var 延迟 = 0;
var 街道 = window.街道.getText();
var 村居 = window.村居.getText();
var 村子 = window.村子.getText();



function 空地() {

    click("所属街镇")
    sleep(50)
    click(街道)
    sleep(50)
    click("所属村居")
    sleep(50)
    click(村居)
    sleep(50)

    setText([1], 村子)
    sleep(10)
    setText([2], 村子 + "村")
    sleep(10)
    setText([3], "不详")
    sleep(10)
    setText([4], "不详")
    sleep(10)
    setText([5], "空地")

    sleep(10)
    click("户主类型")
    sleep(50)
    click("产权人")
    sleep(50)
    click("现场调查情况")
    sleep(50)
    click("不需要调查")
    sleep(50)
    click("确定")
    sleep(50)
    click("原因")
    sleep(50)
    click("图斑")
    sleep(50)

}


function 普查() {
    toastLog("star")
    基本信息()
    建筑信息()
    抗震设防()
    toastLog("end")

}

function 基本信息() {

    click("所属街镇")
    sleep(50)
    click(街道)
    sleep(50)
    click("所属村居")
    sleep(50)
    click(村居)
    sleep(50)

    setText([1], 村子)
    sleep(10)
    setText([2], 村子 + "村")
    sleep(10)
    setText([3], "不详")
    sleep(10)
    setText([4], "不详")
    sleep(10)
    click("户主类型")
    sleep(50)
    click("产权人")
    sleep(50)
    setText([5], "经详细调查，该房屋无裂缝，地基无明显沉降，房屋无倾斜情况。")
}

function 建筑信息() {
    click("建筑信息")
    sleep(50)
    click("安全鉴定")
    sleep(50)
    click("否")
    sleep(50)
    click("建设方式")
    sleep(50)
    click("建筑工匠")
    sleep(50)
    click("结构类型")
    sleep(50)
    click("砖石结构")
    sleep(50)
    click("承重墙体")
    sleep(50)
    click("砖")
    sleep(50)
    click("楼屋盖")
    sleep(50)
    click("现浇板")
    sleep(50)
    back()
    sleep(50)
    click("砌体结构")
    sleep(50)
    click("否")
    sleep(50)
    click("建造年代")
    sleep(50)
    click("2001-2010年")
}

function 抗震设防() {
    sleep(50)
    click("抗震设防信息")
    sleep(50)
    click("专业设计")
    sleep(50)
    click("否")
    sleep(50)
    click("采取抗")
    sleep(50)
    click("是")
    sleep(50)
    click("加固")
    sleep(50)
    click("否")
    sleep(50)
    click("裂缝")
    sleep(50)
    click("无")
    sleep(50)
    click(370, 800)
    sleep(50)
    click("圈梁")
    sleep(50)
    click("钢筋")
    sleep(50)
    back();

}
var x = 0,
    y = 0;
//记录按键被按下时的悬浮窗位置
var windowX, windowY;
//记录按键被按下的时间以便判断长按等动作
var downTime;

window.action.setOnTouchListener(function(view, event) {
    switch (event.getAction()) {
        case event.ACTION_DOWN:
            x = event.getRawX();
            y = event.getRawY();
            windowX = window.getX();
            windowY = window.getY();
            downTime = new Date().getTime();
            return true;
        case event.ACTION_MOVE:
            //移动手指时调整悬浮窗位置
            window.setPosition(windowX + (event.getRawX() - x),
                windowY + (event.getRawY() - y));
            //如果按下的时间超过1.5秒判断为长按，退出脚本
            if (new Date().getTime() - downTime > 1500) {
                exit();
            }
            return true;
        case event.ACTION_UP:
            //手指弹起时如果偏移很小则判断为点击
            if (Math.abs(event.getRawY() - y) < 5 && Math.abs(event.getRawX() - x) < 5) {

                toast("长按可关闭");
            }
            return true;
    }
    return true;
});